using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using MacomberMap.Common.Database_Interface;

namespace MacomberMap.Loader.User_Interfaces.One_Lines
{
    /// <summary>
    /// This form allows the user to choose a one-line to view from a list of available one-lines
    /// </summary>
    public partial class frmOneLineChooser : Form
    {
        #region Variable declarations
        /// <summary>The one-line selected by the user</summary>
        public XmlDocument SelectedOneLine = new XmlDocument();

        /// <summary>The database connection</summary>
        private MM_Database DbConnection;
        #endregion

        #region Initialization
        /// <summary>
        /// Initialize the one-line chooser
        /// </summary>
        /// <param name="DbConnection">The database connection</param>
        public frmOneLineChooser(MM_Database DbConnection)
        {
            this.DbConnection = DbConnection;
            InitializeComponent();
            lvOneLines.Groups.Add("Substations", "Substations");
            lvOneLines.Groups.Add("Lines", "Lines");
            lvOneLines.Columns.Add("Name", "Name");
            lvOneLines.Columns.Add("TEID", "TEID");

            lvOneLines.View = View.Tile;
        }

        /// <summary>
        /// When the form is shown, load our collection of onelines
        /// </summary>
        /// <param name="e"></param>
        protected override void OnShown(EventArgs e)
        {
            foreach (DataRow dr in DbConnection.LoadOneLines().Rows)
            {
                ListViewItem NewItem = lvOneLines.Items.Add((String)dr["NAME"]);
                NewItem.SubItems.Add(Convert.ToUInt64(dr["BASE_ELEMENT"]).ToString("#,##0"));                
                NewItem.Group = lvOneLines.Groups[Convert.ToInt32(dr["TYPE"])];                    
                NewItem.Tag = dr;
            }
            base.OnShown(e);
        }
        #endregion

        #region User Interactions
        private void btnOK_Click(object sender, EventArgs e)
        {
            if (lvOneLines.SelectedItems.Count == 1)
            {
                SelectedOneLine.LoadXml(DbConnection.LoadOneLine(Convert.ToUInt64((lvOneLines.SelectedItems[0].Tag as DataRow)["BASE_ELEMENT"])));
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }
        #endregion
    }
}